// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// [START storage_set_metadata]
use google_cloud_storage::client::StorageControl;
use google_cloud_wkt::FieldMask;

pub async fn sample(client: &StorageControl, bucket_id: &str) -> anyhow::Result<()> {
    const NAME: &str = "object-to-update";
    let object = client
        .get_object()
        .set_bucket(format!("projects/_/buckets/{bucket_id}"))
        .set_object(NAME)
        .send()
        .await?;

    let metageneration = object.metageneration;
    let mut meta = object.metadata.clone();
    meta.insert("updated".to_string(), "true".to_string());

    let updated = client
        .update_object()
        .set_if_metageneration_match(metageneration)
        .set_object(object.set_metadata(meta))
        .set_update_mask(FieldMask::default().set_paths(["metadata"]))
        .send()
        .await?;
    println!("successfully updated object {NAME} in bucket {bucket_id}: {updated:?}");
    Ok(())
}
// [END storage_set_metadata]
